 //
//  IWAccoutTool.m
//  SINA
//
//  Created by 王相超 on 16/3/31.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

#import "IWAccoutTool.h"
#import "IWAccount.h"


@implementation IWAccoutTool

+(void)saveAccount:(IWAccount *)model{
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"account.archiver"];
    
    [NSKeyedArchiver archiveRootObject:model toFile:path];
    
}

+(IWAccount *)model{
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"account.archiver"];
    
    IWAccount *accout = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (accout == nil) {
        return nil;
    }
    
    NSDate *date = [accout.creatDate dateByAddingTimeInterval:accout.expires_in];
    
    NSDate *currentDate = [NSDate date];
    
    if ([currentDate compare:date] != NSOrderedAscending) {
        
        accout = nil;
    }

    
    return [NSKeyedUnarchiver unarchiveObjectWithFile:path];
}

@end
